package com.quanquan.user.service;

import DTO.PageDTO;
import com.quanquan.user.dto.SignInRuleDTO;
import com.quanquan.user.entity.SignInRule;
import com.quanquan.user.exception.SignInRuleException;
import com.quanquan.user.repository.SignInRuleRepository;
import com.quanquan.user.request.SignRuleInRequestBody;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import request.PageRequestBody;
import utils.DateUtils;
import utils.UUIDGenerator;

/**
 * 功能描述: 签到积分规则
 *
 * @ClassName: SignInRuleService
 * @Author: xiaoqianh丶
 * @Date: 2019-04-05 21:53
 * @Version: V1.0
 */
@Service
public class SignInRuleService {

    private final SignInRuleRepository signInRuleRepository;

    public SignInRuleService(SignInRuleRepository signInRuleRepository) {
        this.signInRuleRepository = signInRuleRepository;
    }


    public SignInRule findSignInRule(){
        return signInRuleRepository.findByUseStatus(true);
    }


    public PageDTO<SignInRule> allSignInRuleForManagement(PageRequestBody requestBody) throws SignInRuleException {
        Integer size = requestBody.getSize();
        Integer page = requestBody.getPage();

        Pageable pageable =  PageRequest.of(page, size);
        SignInRule signInRule = new SignInRule();
        signInRule.setRemove(false);
        Example<SignInRule> example = Example.of(signInRule);
        Page<SignInRule> findAll = signInRuleRepository.findAll(example, pageable);
        if ((findAll.getSize()) == 0) {
            throw new SignInRuleException("签到积分规则列表没数据");
        }
        PageDTO<SignInRule> pageDTO = new PageDTO<>();
        pageDTO.setTotal(findAll.getTotalElements());
        pageDTO.setList(findAll.getContent());
        pageDTO.setTotalPage(findAll.getTotalPages());
        pageDTO.setPage(page);
        pageDTO.setPageSize(size);
        return pageDTO;
    }

    /**
     * 创建签到积分规则
     * @param requestBody
     */
    public String createRule(SignRuleInRequestBody requestBody) {
        SignInRule signInRule = new SignInRule();
        signInRule.setId(UUIDGenerator.getUUID());
        signInRule.setUseStatus(requestBody.getUseStatus());
        signInRule.setConsecutiveDay(requestBody.getConsecutiveDay());
        signInRule.setCreateTime(DateUtils.currentDate());
        signInRule.setUpdateTime(DateUtils.currentDate());
        signInRule.setIntegralPoint(requestBody.getIntegralPoint());
        signInRule.setRewardType(requestBody.getRewardType());
        if(requestBody.getRewardType()==0){
            signInRule.setBonusPoints(requestBody.getBonusPoints());
        }else{
            signInRule.setCouponId(requestBody.getCouponId());
        }
        signInRule.setRemove(false);
        signInRule.setBonusSecondPoints(requestBody.getBonusSecondPoints());
        signInRule.setConsecutiveSecondDay(requestBody.getConsecutiveSecondDay());
        System.out.println("requestBody..."+requestBody);
        signInRuleRepository.save(signInRule);
        //如果新增的规则要应用其他的规则自动设置为不应用
        if(requestBody.getUseStatus()){
            signInRuleRepository.removeUseStatus(signInRule.getId());
        }
        return "添加规则成功";
    }

    /**
     * 查询应用的规则
     */
    public SignInRule findUsedRule() {
        SignInRule signInRule = signInRuleRepository.findByUseStatus(true);
        return signInRule;
    }


    public boolean updateSignInRule(SignRuleInRequestBody request) {


        String signInRuleId = request.getId();
        SignInRule signInRule = signInRuleRepository.findByIdAndRemove(signInRuleId, false);
        if (signInRule==null){
            return false;
        }
        signInRule.setConsecutiveDay(request.getConsecutiveDay());
        signInRule.setIntegralPoint(request.getIntegralPoint());
        signInRule.setUpdateTime(DateUtils.currentDate());
        signInRule.setUseStatus(request.getUseStatus());
        signInRule.setRewardType(request.getRewardType());
        if(request.getRewardType()==0){
            signInRule.setBonusPoints(request.getBonusPoints());
        }else{
            signInRule.setCouponId(request.getCouponId());
        }
        signInRule.setBonusSecondPoints(request.getBonusSecondPoints());
        signInRule.setConsecutiveSecondDay(request.getConsecutiveSecondDay());
        signInRuleRepository.save(signInRule);
        //如果新增的规则要应用其他的规则自动设置为不应用
        if(request.getUseStatus()){
            signInRuleRepository.removeUseStatus(signInRule.getId());
        }
        return true;
    }

    public boolean removeSignInRule(SignRuleInRequestBody requestBody) {

        String signInRuleId = requestBody.getId();
        SignInRule signInRule = signInRuleRepository.findByIdAndRemove(signInRuleId, false);
        if (signInRule==null){
            return false;
        }
        signInRule.setRemove(true);
        signInRuleRepository.save(signInRule);
        return true;
    }

    public SignInRuleDTO query(SignRuleInRequestBody request) {
        SignInRule signInRule = signInRuleRepository.findByIdAndRemove(request.getId(), false);
        SignInRuleDTO signInRuleDTO = new SignInRuleDTO();
        //通过映射关系来赋值 //TODO 断点看一下
        BeanUtils.copyProperties(signInRule, signInRuleDTO);
        return signInRuleDTO;
    }

}
